# 五子棋游戏 HTML 实现

以下是一个完整的五子棋游戏 HTML 实现，所有代码都在一个 HTML 文件中：

```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>五子棋游戏</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        
        h1 {
            color: #333;
            margin-bottom: 10px;
        }
        
        .game-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 20px;
        }
        
        .game-info {
            margin-bottom: 15px;
            font-size: 18px;
            color: #333;
        }
        
        .board {
            display: grid;
            grid-template-columns: repeat(15, 30px);
            grid-template-rows: repeat(15, 30px);
            background-color: #dcb35c;
            border: 2px solid #8d6e3a;
            position: relative;
        }
        
        .cell {
            width: 30px;
            height: 30px;
            box-sizing: border-box;
            position: relative;
            cursor: pointer;
        }
        
        .cell::before, .cell::after {
            content: '';
            position: absolute;
            background-color: #000;
        }
        
        .cell::before {
            width: 100%;
            height: 1px;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
        }
        
        .cell::after {
            width: 1px;
            height: 100%;
            left: 50%;
            top: 0;
            transform: translateX(-50%);
        }
        
        .cell.black::before, .cell.white::before,
        .cell.black::after, .cell.white::after {
            display: none;
        }
        
        .black, .white {
            width: 26px;
            height: 26px;
            border-radius: 50%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        
        .black {
            background: radial-gradient(circle at 30% 30%, #666, #000);
            box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
        }
        
        .white {
            background: radial-gradient(circle at 30% 30%, #fff, #ccc);
            box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        }
        
        .controls {
            margin-top: 20px;
        }
        
        button {
            padding: 8px 16px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 0 5px;
        }
        
        button:hover {
            background-color: #45a049;
        }
        
        .win-message {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 20px 40px;
            border-radius: 10px;
            font-size: 24px;
            z-index: 100;
            display: none;
        }
        
        .win-message.show {
            display: block;
            animation: fadeIn 0.5s;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
    </style>
</head>
<body>
    <h1>五子棋游戏</h1>
    <div class="game-container">
        <div class="game-info" id="game-info">当前回合: 黑棋</div>
        <div class="board" id="board"></div>
        <div class="controls">
            <button id="restart-btn">重新开始</button>
        </div>
    </div>
    <div class="win-message" id="win-message"></div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const board = document.getElementById('board');
            const gameInfo = document.getElementById('game-info');
            const restartBtn = document.getElementById('restart-btn');
            const winMessage = document.getElementById('win-message');
            
            const BOARD_SIZE = 15;
            let currentPlayer = 'black'; // 'black' 或 'white'
            let gameBoard = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
            let gameOver = false;
            
            // 初始化棋盘
            function initBoard() {
                board.innerHTML = '';
                
                for (let i = 0; i < BOARD_SIZE; i++) {
                    for (let j = 0; j < BOARD_SIZE; j++) {
                        const cell = document.createElement('div');
                        cell.className = 'cell';
                        cell.dataset.row = i;
                        cell.dataset.col = j;
                        
                        cell.addEventListener('click', () => handleCellClick(i, j));
                        board.appendChild(cell);
                    }
                }
            }
            
            // 处理点击事件
            function handleCellClick(row, col) {
                if (gameOver || gameBoard[row][col] !== null) return;
                
                gameBoard[row][col] = currentPlayer;
                renderMove(row, col, currentPlayer);
                
                if (checkWin(row, col)) {
                    gameOver = true;
                    const winner = currentPlayer === 'black' ? '黑棋' : '白棋';
                    winMessage.textContent = `${winner}获胜！`;
                    winMessage.classList.add('show');
                    return;
                }
                
                // 切换玩家
                currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
                updateGameInfo();
            }
            
            // 渲染棋子
            function renderMove(row, col, player) {
                const cell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);
                const piece = document.createElement('div');
                piece.className = player;
                cell.appendChild(piece);
            }
            
            // 检查是否获胜
            function checkWin(row, col) {
                const directions = [
                    [0, 1],   // 水平
                    [1, 0],   // 垂直
                    [1, 1],   // 对角线
                    [1, -1]    // 反对角线
                ];
                
                for (const [dx, dy] of directions) {
                    let count = 1;
                    
                    // 正向检查
                    for (let i = 1; i < 5; i++) {
                        const newRow = row + i * dx;
                        const newCol = col + i * dy;
                        
                        if (
                            newRow >= 0 && newRow < BOARD_SIZE &&
                            newCol >= 0 && newCol < BOARD_SIZE &&
                            gameBoard[newRow][newCol] === currentPlayer
                        ) {
                            count++;
                        } else {
                            break;
                        }
                    }
                    
                    // 反向检查
                    for (let i = 1; i < 5; i++) {
                        const newRow = row - i * dx;
                        const newCol = col - i * dy;
                        
                        if (
                            newRow >= 0 && newRow < BOARD_SIZE &&
                            newCol >= 0 && newCol < BOARD_SIZE &&
                            gameBoard[newRow][newCol] === currentPlayer
                        ) {
                            count++;
                        } else {
                            break;
                        }
                    }
                    
                    if (count >= 5) {
                        return true;
                    }
                }
                
                return false;
            }
            
            // 更新游戏信息
            function updateGameInfo() {
                const player = currentPlayer === 'black' ? '黑棋' : '白棋';
                gameInfo.textContent = `当前回合: ${player}`;
            }
            
            // 重新开始游戏
            function restartGame() {
                currentPlayer = 'black';
                gameBoard = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
                gameOver = false;
                initBoard();
                updateGameInfo();
                winMessage.classList.remove('show');
            }
            
            // 事件监听
            restartBtn.addEventListener('click', restartGame);
            
            // 初始化游戏
            initBoard();
        });
    </script>
</body>
</html>
```

## 游戏功能说明

1. **基本规则**：
   - 15×15 标准五子棋棋盘
   - 黑棋先行，轮流落子
   - 先形成五子连珠的一方获胜

2. **界面功能**：
   - 显示当前回合玩家
   - 棋子视觉效果（带阴影和渐变）
   - 获胜提示弹窗
   - 重新开始按钮

3. **游戏逻辑**：
   - 检查水平、垂直、对角线方向的五子连珠
   - 防止重复落子
   - 游戏结束后禁止继续落子

您可以将此代码保存为单个 HTML 文件，然后在浏览器中打开即可开始游戏。游戏完全在客户端运行，不需要任何服务器支持。